#!/usr/bin/sh

modprobe vfd       
echo "    Welcome     " > /dev/vfd

if [ -e /var/etc/.firstboot ]; then
   echo "WAIT 1 min" > /dev/vfd
   echo "WAIT 1 min"
   rm /var/etc/.firstboot
   touch /dev/.devfsd
fi

if [ -e /etc/init.d/mountvirtfs ]; then
   /etc/init.d/mountvirtfs start
fi

if [ ! -e /var/etc/.firstboot ]; then

   modprobe e2_proc

   # mme bug workaround
   /usr/bin/dd if=/dev/zero of=/dev/st231-0 bs=1024 count=4096
   /usr/bin/dd if=/dev/zero of=/dev/st231-1 bs=1024 count=4096
   # end mme bug wa
   echo "load a firmware"
   /usr/bin/ustslave /dev/st231-1 /boot/audio.elf
   echo "load v firmware"
   /usr/bin/ustslave /dev/st231-0 /boot/video.elf

   echo "Starting DEBUG Interface"
if [ -e /etc/init.d/mountall ]; then
   /etc/init.d/mountall start
fi

   #used by fp_control! otherwise crash
   modprobe boxtype

   echo "LOADING MODULES"   
   fp_control -c
   
   echo "init embxshell"
   modprobe embxshell
   echo "init embxmailbox"
   modprobe embxmailbox mailbox0=0xb9211000:136:set2 mailbox1=0xb9212000:137:set2 ;
   echo "init embxshm"
   modprobe embxshm mailbox0=shm:0:7:0x60000000:0:256:256:0:512:0xa4000000:0x10000000 ;
   echo "init mme_host"
   modprobe mme_host transport0=shm

   echo "init AVS"
   modprobe avs
   echo "init stmfb"
   modprobe stmcore-display-stx7100
   modprobe stmfb display0=1280x720-32@50:8m:pal:yuv:yuv;# display1=720x576-16@50:1m:pal:cvbs;

   echo "init player"
   player=`cat /proc/stb/player/version`
   if [ $player=='player191' ]; then
      echo "init player 191"
   elif [ $player=='player179' ]; then
      echo "init player 179"
   else
      echo "init player 131"
   fi
   modprobe mmelog
   modprobe mpeg2hw mmeName=MPEG2_TRANSFORMER0;
   modprobe stmsysfs
   modprobe ksound
   modprobe p2div64
   modprobe stm_v4l2
   modprobe pseudocard
   if [ $player=='player179' ] || [ $player=='player191' ]; then
      modprobe stmvout
      modprobe stmvbi
   fi

   modprobe cx24116
   modprobe cimax

   modprobe pti
   modprobe stm_monitor
   modprobe stmdvb

   modprobe player2
   modprobe sth264pp
   modprobe stmalloc
   modprobe platform
   modprobe silencegen
   modprobe bpamem

   echo "init lircd"
   mkdir -p /var/run/lirc
   /usr/bin/lircd

   echo "init frontpanel"
   modprobe simu_button
   /usr/bin/evremote2 &
   var=`cat /proc/boxtype`
   case "$var" in
   	0) echo "1W boxtype"
	   echo "B" > /dev/ttyAS1
           echo "B" > /dev/ttyAS1;;
  	1|3) echo "14W boxtype"
      	   modprobe button
      	   modprobe led;;
   	*) echo "unknown boxtype";;
   esac

if [ -e /etc/init.d/portmap ]; then
   /etc/init.d/portmap start
fi
if [ -e /etc/init.d/vsftpd ]; then
   /etc/init.d/vsftpd start
fi
   /usr/sbin/inetd

   #timefix e2
   date -s "200901011200"

   stfbcontrol a 255

   echo "LOADING Enigma2" > /dev/vfd
   echo "LOADING Enigma2"

   until false
   do
      echo "starting Enigma2 ->"
      /usr/bin/enigma2
      rtv=$?
      echo "enigma2 ended <- RTV: " $rtv
      case "$rtv" in
           0)  echo "E2 ERROR"
               echo "E2 Error" > /dev/vfd
               init 6;;
           1)  echo "1"
               echo "SHUTDOWN"
               echo "Shutdown" > /dev/vfd
               if [ -e /usr/share/shutdown.mvi ]; then
                    showiframe -p /usr/share/shutdown.mvi &
               fi
               init 0;;
           2)  echo "2"
               echo "REBOOT"
               echo "Reboot" > /dev/vfd
               init 6;;
           3)  echo "3"
               echo "E2 RESTART"
               echo "Restart GUI" > /dev/vfd
               echo "0" > /proc/progress
               if [ -e /usr/share/backdrop.mvi ]; then
                    showiframe -p /usr/share/backdrop.mvi &
               else
                    showiframe -p /usr/share/bootlogo.mvi &
               fi;;
           *)  echo "*"
               echo "ERROR"
               echo "Error" > /dev/vfd
               init 6;;
      esac
   done 
fi
